home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / wifisetupdialog.py < prev    next >
Text File  |  2009-10-09  |  30KB  |  825 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Authors: Don Welch
  20. #
  21.  
  22. # StdLib
  23. import operator
  24.  
  25. # Local
  26. from base.g import *
  27. from base import device, utils, models, wifi
  28. from base.codes import *
  29. from ui_utils import *
  30.  
  31. # Qt
  32. from PyQt4.QtCore import *
  33. from PyQt4.QtGui import *
  34.  
  35. # Ui
  36. from wifisetupdialog_base import Ui_Dialog
  37.  
  38.  
  39.  
  40. PAGE_INTRO = 0 # Ask user to plugin temp USB connection
  41. PAGE_DEVICES = 1 # Select a probed USB device
  42. PAGE_NETWORK = 2 # Select a discovered SSID
  43. PAGE_CONFIGURE_WIFI = 3 # Configure USB device on network
  44. PAGE_EXIT = 4 #  Tell user if successful, unplug USB onnection
  45.  
  46.  
  47. BUTTON_NEXT = 0
  48. BUTTON_FINISH = 1
  49. BUTTON_CONNECT = 3
  50.  
  51. SUCCESS_NOT_CONNECTED = 0
  52. SUCCESS_AUTO_IP = 1
  53. SUCCESS_CONNECTED = 2
  54.  
  55. ASSOCIATE_DELAY = 30
  56. REFRESH_INTERVAL = 20
  57.  
  58.  
  59. class DeviceTableWidgetItem(QTableWidgetItem):
  60.     def __init__(self, text, device_uri):
  61.         QTableWidgetItem.__init__(self, text, QTableWidgetItem.UserType)
  62.         self.device_uri = device_uri
  63.  
  64.  
  65. class WifiSetupDialog(QDialog, Ui_Dialog):
  66.     def __init__(self, parent, device_uri=None, standalone=True):
  67.         QDialog.__init__(self, parent)
  68.         self.setupUi(self)
  69.  
  70.         self.device_uri = device_uri
  71.         self.devices = {}
  72.         self.networks = {}
  73.         self.ssid = ''
  74.         self.directed = False
  75.         self.show_extended = False
  76.         self.bus = 'usb'
  77.         self.search = ''
  78.         self.max_page = PAGE_EXIT
  79.         self.location_cache = {} # { 'bssid' : <location>, ... }
  80.         self.dev = None
  81.         self.success = SUCCESS_NOT_CONNECTED
  82.         self.ip = '0.0.0.0'
  83.         self.hn = ''
  84.         self.standalone = standalone
  85.         self.initUi()
  86.  
  87.         if self.device_uri is None:
  88.             QTimer.singleShot(0, self.showIntroPage)
  89.         else:
  90.             QTimer.singleShot(0, self.showNetworkPage)
  91.  
  92.  
  93.     #
  94.     # INIT
  95.     #
  96.  
  97.     def initUi(self):
  98.         self.setWindowIcon(QIcon(load_pixmap('hp_logo', '128x128')))
  99.  
  100.         # connect signals/slots
  101.         self.connect(self.CancelButton, SIGNAL("clicked()"), self.CancelButton_clicked)
  102.         self.connect(self.BackButton, SIGNAL("clicked()"), self.BackButton_clicked)
  103.         self.connect(self.NextButton, SIGNAL("clicked()"), self.NextButton_clicked)
  104.  
  105.         self.initIntroPage()
  106.         self.initDevicesPage()
  107.         self.initNetworkPage()
  108.         self.initConfigWifiPage()
  109.         self.initExitPage()
  110.  
  111.     #
  112.     # INTRO PAGE
  113.     #
  114.  
  115.     def initIntroPage(self):
  116.         self.Picture.setPixmap(load_pixmap("usb_connection", "other"))
  117.         self.InfoIcon.setPixmap(load_pixmap("info", "16x16"))
  118.  
  119.         if self.standalone:
  120.             self.MainTitleLabel.setText(self.__tr("""This utility allows you configure your wireless capable printer using a temporary USB connection. You will be prompted to disconnect the USB cable once wireless network setup is complete.
  121.  
  122. <p><i>Note: This configuration utility does not setup (install) your printer on this computer. Use hp-setup to setup your printer after it has been configured on the network by this utility.</i></p>
  123. <p><i>Note: Only select wireless capable printers are supported by this utility.</i></p>"""))
  124.         else:
  125.             self.MainTitleLabel.setText(self.__tr("""This utility allows you configure your wireless capable printer using a temporary USB connection. You will be prompted to disconnect the USB cable once wireless network setup is complete.
  126.  
  127. <p><i>Note: Printer setup (installation) will continue after your printer is configured on the network.</i></p>
  128. <p><i>Note: Only select wireless capable printers are supported by this utility.</i></p>"""))
  129.  
  130.  
  131.     def showIntroPage(self):
  132.         self.BackButton.setEnabled(False)
  133.         self.NextButton.setEnabled(True)
  134.  
  135.         self.displayPage(PAGE_INTRO)
  136.  
  137.  
  138.     #
  139.     # DEVICES PAGE
  140.     #
  141.  
  142.     def initDevicesPage(self):
  143.         self.connect(self.RefreshButton,  SIGNAL("clicked()"),  self.RefreshButton_clicked)
  144.  
  145.  
  146.     def showDevicesPage(self):
  147.         self.BackButton.setEnabled(True)
  148.         self.setNextButton(BUTTON_NEXT)
  149.  
  150.         beginWaitCursor()
  151.         try:
  152.             if not self.devices:
  153.                 log.info("Searching on USB bus...")
  154.                 filter_dict = {'wifi-config' : (operator.gt, WIFI_CONFIG_NONE)}
  155.  
  156.                 self.devices = device.probeDevices([self.bus], 0, 0, filter_dict, self.search)
  157.         finally:
  158.             endWaitCursor()
  159.  
  160.         self.clearDevicesTable()
  161.  
  162.         if self.devices:
  163.             self.NextButton.setEnabled(True)
  164.             self.DevicesFoundIcon.setPixmap(load_pixmap('info', '16x16'))
  165.  
  166.             if len(self.devices) == 1:
  167.                 self.DevicesFoundLabel.setText(self.__tr("<b>1 wireless capable device found.</b> Click <i>Next</i> to continue."))
  168.             else:
  169.                 self.DevicesFoundLabel.setText(self.__tr("<b>%1 wireless capable devices found.</b> Select the device to install and click <i>Next</i> to continue.").arg(len(self.devices)))
  170.  
  171.             self.loadDevicesTable()
  172.  
  173.         else:
  174.             self.NextButton.setEnabled(False)
  175.             self.DevicesFoundIcon.setPixmap(load_pixmap('error', '16x16'))
  176.             log.error("No devices found on bus: %s" % self.bus)
  177.             self.DevicesFoundLabel.setText(self.__tr("<b>No wireless capable devices found.</b><br>Plug in your printer with a USB cable and click <i>Refresh</i> to search again."))
  178.  
  179.         self.displayPage(PAGE_DEVICES)
  180.  
  181.  
  182.     def loadDevicesTable(self):
  183.         self.DevicesTableWidget.setSortingEnabled(False)
  184.         self.DevicesTableWidget.setRowCount(len(self.devices))
  185.  
  186.         headers = [self.__tr('Model'), self.__tr('Device URI')]
  187.         device_uri_col = 1
  188.  
  189.         self.DevicesTableWidget.setColumnCount(len(headers))
  190.         self.DevicesTableWidget.setHorizontalHeaderLabels(headers)
  191.         flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
  192.  
  193.         for row, d in enumerate(self.devices):
  194.             back_end, is_hp, bus, model, serial, dev_file, host, zc, port = device.parseDeviceURI(d)
  195.             model_ui = models.normalizeModelUIName(model)
  196.  
  197.             i = DeviceTableWidgetItem(QString(model_ui), d)
  198.             i.setFlags(flags)
  199.             self.DevicesTableWidget.setItem(row, 0, i)
  200.  
  201.             i = QTableWidgetItem(QString(d))
  202.             i.setFlags(flags)
  203.             self.DevicesTableWidget.setItem(row, device_uri_col, i)
  204.  
  205.         self.DevicesTableWidget.resizeColumnsToContents()
  206.         self.DevicesTableWidget.setSortingEnabled(True)
  207.         self.DevicesTableWidget.sortItems(0)
  208.         self.DevicesTableWidget.selectRow(0)
  209.  
  210.  
  211.     def clearDevicesTable(self):
  212.         self.DevicesTableWidget.clear()
  213.         self.DevicesTableWidget.setRowCount(0)
  214.         self.DevicesTableWidget.setColumnCount(0)
  215.  
  216.  
  217.     def RefreshButton_clicked(self):
  218.         self.clearDevicesTable()
  219.         self.devices = []
  220.         QTimer.singleShot(0, self.showDevicesPage)
  221.  
  222.  
  223.     #
  224.     # NETWORK
  225.     #
  226.  
  227.     def initNetworkPage(self):
  228.         self.NetworksTableWidget.setIconSize(QSize(34, 20))
  229.         self.ShowExtendedCheckBox.setChecked(False)
  230.         self.connect(self.ShowExtendedCheckBox, SIGNAL("clicked(bool)"), self.ShowExtendedCheckBox_clicked)
  231.         self.connect(self.SearchPushButton, SIGNAL("clicked()"), self.SearchPushButton_clicked)
  232.         self.connect(self.UndirectedRadioButton, SIGNAL("clicked(bool)"), self.UndirectedRadioButton_clicked)
  233.         self.connect(self.DirectedRadioButton, SIGNAL("clicked(bool)"), self.DirectedRadioButton_clicked)
  234.         self.connect(self.NetworksTableWidget, SIGNAL("itemSelectionChanged()"), self.NetworksTableWidget_itemSelectionChanged)
  235.  
  236.  
  237.     def showNetworkPage(self):
  238.         if self.dev is None:
  239.             try:
  240.                 self.dev = device.Device(self.device_uri)
  241.             except Error, e:
  242.                 FailureUI(self, self.__tr("<b>Error opening device:</b><p>%1</p><p>(%2)</p>").arg(self.device_uri).arg(QString(e[0])))
  243.  
  244.                 if self.dev is not None:
  245.                     self.dev.close()
  246.  
  247.                 self.close()
  248.                 return
  249.  
  250.         self.networks.clear()
  251.         self.num_networks = 0
  252.  
  253.         try:
  254.             self.adaptor_id, name, state, presence = wifi.getWifiAdaptorID(self.dev)
  255.         except Error, e:
  256.             self.showIOError(e)
  257.             return
  258.  
  259.         log.debug("Adaptor ID: %s" % self.adaptor_id)
  260.         log.debug("Adaptor name: %s" % name)
  261.         log.debug("Adaptor state: %s" % state)
  262.         log.debug("Adaptor presence: %s" % presence)
  263.  
  264.         if self.adaptor_id == -1:
  265.             FailureUI(self, self.__tr("<b>Unable to locate wireless hardware on device.</b>"))
  266.             if self.dev is not None:
  267.                 self.dev.close()
  268.  
  269.             self.close()
  270.  
  271.         log.debug("Turning on wireless radio...")
  272.         try:
  273.             wifi.setAdaptorPower(self.dev, self.adaptor_id)
  274.         except Error, e:
  275.             self.showIOError(e)
  276.             return
  277.  
  278.         self.performScan()
  279.         self.setNextButton(BUTTON_NEXT)
  280.         self.displayPage(PAGE_NETWORK)
  281.  
  282.  
  283.     def performScan(self):
  284.         beginWaitCursor()
  285.         error = False
  286.         try:
  287.             self.ssid = unicode(self.SSIDLineEdit.text())
  288.             if self.directed and self.ssid:
  289.                 try:
  290.                     self.networks = wifi.performScan(self.dev, self.ssid)
  291.                 except Error, e:
  292.                     self.showIOError(e)
  293.                     return
  294.             else:
  295.                 try:
  296.                     self.networks = wifi.performScan(self.dev)
  297.                 except Error, e:
  298.                     self.showIOError(e)
  299.                     return
  300.         finally:
  301.             self.dev.close()
  302.             endWaitCursor()
  303.  
  304.         self.num_networks = self.networks['numberofscanentries']
  305.         self.clearNetworksTable()
  306.  
  307.         if self.num_networks:
  308.             self.NextButton.setEnabled(True)
  309.             self.NetworksFoundIcon.setPixmap(load_pixmap('info', '16x16'))
  310.  
  311.             if self.num_networks == 1:
  312.                 self.NetworksFoundLabel.setText(self.__tr("<b>1 wireless network found. </b> If the wireless network you would like to connect to is not listed, try entering a wireless network name and/or press <i>Search</i> to search again."))
  313.             else:
  314.                 self.NetworksFoundLabel.setText(self.__tr("<b>%1 wireless networks found.</b> If the wireless network you would like to connect to is not listed, try entering a wireless network name and/or press <i>Search</i> to search again.").arg(self.num_networks))
  315.  
  316.             self.loadNetworksTable()
  317.  
  318.         else:
  319.             self.NextButton.setEnabled(False)
  320.             self.NetworksFoundIcon.setPixmap(load_pixmap('error', '16x16'))
  321.             log.warning("No wireless networks found.")
  322.             self.NetworksFoundLabel.setText(self.__tr("<b>No wireless networks found.</b><br>Enter a wireless network name and/or press <i>Search</i> to search again."))
  323.  
  324.  
  325.     def ShowExtendedCheckBox_clicked(self, b):
  326.         self.show_extended = b
  327.         self.loadNetworksTable()
  328.  
  329.  
  330.     def SearchPushButton_clicked(self):
  331.         self.performScan()
  332.         self.loadNetworksTable()
  333.  
  334.  
  335.     def UndirectedRadioButton_clicked(self, b):
  336.         self.directed = not b
  337.         self.SSIDLineEdit.setEnabled(not b)
  338.  
  339.  
  340.     def DirectedRadioButton_clicked(self, b):
  341.         self.directed = b
  342.         self.SSIDLineEdit.setEnabled(b)
  343.  
  344.  
  345.     def loadNetworksTable(self):
  346.         self.n, self.network = 0, u''
  347.         if self.num_networks:
  348.             beginWaitCursor()
  349.             try:
  350.                 if self.show_extended:
  351.                     for n in xrange(self.num_networks):
  352.                         bssid = self.networks['bssid-%d' % n]
  353.                         ss = self.networks['signalstrength-%d' % n]
  354.                         try:
  355.                             self.location_cache[bssid]
  356.                         except KeyError:
  357.                             location = wifi.getLocation(bssid, ss)
  358.                             lat = self.networks['latitude-%d' % n] = location.get('latitude', 'Unknown')
  359.                             lng  = self.networks['longitude-%d' % n] = location.get('longitude', 'Unknown')
  360.                             address = self.networks['address-%d' % n] = location.get('address', 'Unknown')
  361.                             self.location_cache[bssid] = (lat, lng, address)
  362.                         else:
  363.                             self.networks['latitude-%d' % n], self.networks['longitude-%d' % n], self.networks['address-%d' % n] = \
  364.                                 self.location_cache[bssid]
  365.  
  366.                 self.NetworksTableWidget.setSortingEnabled(False)
  367.                 self.NetworksTableWidget.setRowCount(self.num_networks)
  368.  
  369.                 headers = [self.__tr('Network Name (SSID)'), self.__tr('Signal Strength'),
  370.                             self.__tr("Security"), self.__tr("Mode")]
  371.  
  372.                 if self.show_extended:
  373.                     headers.extend([self.__tr('Channel'),
  374.                             self.__tr("Address (BSSID)"), self.__tr("Location"),
  375.                             self.__tr("Lat/Long")])
  376.  
  377.                 self.NetworksTableWidget.setColumnCount(len(headers))
  378.                 self.NetworksTableWidget.setHorizontalHeaderLabels(headers)
  379.                 enabled_flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
  380.  
  381.                 for n in xrange(self.num_networks):
  382.                     name = self.networks['ssid-%d' % n]
  383.  
  384.                     if name == '(unknown)':
  385.                         flags = None
  386.                     else:
  387.                         flags = enabled_flags
  388.  
  389.                     ss = self.networks['signalstrength-%d' % n]
  390.                     sec = self.networks['encryptiontype-%d' % n]
  391.                     mode = self.networks['communicationmode-%d' % n]
  392.  
  393.                     log.debug("%d: Name=%s, strength=%s, security=%s, mode=%s" % #, channel=%d bssid=%s" %
  394.                         (n, name, ss, sec, mode))
  395.  
  396.                     if self.show_extended:
  397.                         chn = self.networks['channel-%d' % n]
  398.                         dbm = self.networks['dbm-%d' % n]
  399.                         bssid = self.networks['bssid-%d' % n]
  400.                         address = self.networks['address-%d' % n]
  401.                         lat = self.networks['latitude-%d' % n]
  402.                         lng = self.networks['longitude-%d' % n]
  403.  
  404.                         log.debug("%d: channel=%d bssid=%s dbm=%s lat=%s long=%s address=%s" %
  405.                             (n, chn, bssid, dbm, lat, lng, address))
  406.  
  407.                     i = QTableWidgetItem(QString(name))
  408.                     if flags is not None: i.setFlags(flags)
  409.                     i.setData(Qt.UserRole, QVariant(n))
  410.                     self.NetworksTableWidget.setItem(n, 0, i)
  411.  
  412.                     pixmap = load_pixmap('signal%d' % ss, 'other')
  413.                     if self.show_extended:
  414.                         i = QTableWidgetItem(QIcon(pixmap), self.__tr("%1/5 (%2 dBm)").arg(ss).arg(dbm))
  415.                     else:
  416.                         i = QTableWidgetItem(QIcon(pixmap), self.__tr("%1/5").arg(ss))
  417.                     if flags is not None: i.setFlags(flags)
  418.                     self.NetworksTableWidget.setItem(n, 1, i)
  419.  
  420.                     i = QTableWidgetItem(QString(sec))
  421.                     if flags is not None: i.setFlags(flags)
  422.                     self.NetworksTableWidget.setItem(n, 2, i)
  423.  
  424.                     i = QTableWidgetItem(QString(mode))
  425.                     if flags is not None: i.setFlags(flags)
  426.                     self.NetworksTableWidget.setItem(n, 3, i)
  427.  
  428.                     if self.show_extended:
  429.                         i = QTableWidgetItem(QString(str(chn)))
  430.                         if flags is not None: i.setFlags(flags)
  431.                         self.NetworksTableWidget.setItem(n, 4, i)
  432.  
  433.                         i = QTableWidgetItem(QString(bssid))
  434.                         if flags is not None: i.setFlags(flags)
  435.                         self.NetworksTableWidget.setItem(n, 5, i)
  436.  
  437.                         i = QTableWidgetItem(QString(address))
  438.                         if flags is not None: i.setFlags(flags)
  439.                         self.NetworksTableWidget.setItem(n, 6, i)
  440.  
  441.                         i = QTableWidgetItem(QString("%1/%2").arg(lat).arg(lng))
  442.                         if flags is not None: i.setFlags(flags)
  443.                         self.NetworksTableWidget.setItem(n, 7, i)
  444.  
  445.  
  446.                 self.NetworksTableWidget.resizeColumnsToContents()
  447.                 self.NetworksTableWidget.setSortingEnabled(True)
  448.                 self.NetworksTableWidget.sortItems(1, Qt.DescendingOrder)
  449.                 self.NetworksTableWidget.selectRow(0)
  450.                 self.NetworksTableWidget.emit(SIGNAL("itemSelectionChanged()"))
  451.  
  452.             finally:
  453.                 endWaitCursor()
  454.                 self.NextButton.setEnabled(True)
  455.  
  456.         else:
  457.             self.NextButton.setEnabled(False)
  458.  
  459.  
  460.     def NetworksTableWidget_itemSelectionChanged(self):
  461.         row = self.NetworksTableWidget.currentRow()
  462.         item = self.NetworksTableWidget.item(row, 0)
  463.         n, ok = item.data(Qt.UserRole).toInt()
  464.         if ok:
  465.             sec = self.networks['encryptiontype-%d' % n]
  466.             if sec.lower() == 'none':
  467.                 self.setNextButton(BUTTON_CONNECT)
  468.             else:
  469.                 self.setNextButton(BUTTON_NEXT)
  470.  
  471.  
  472.     def clearNetworksTable(self):
  473.         self.DevicesTableWidget.clear()
  474.         self.DevicesTableWidget.setRowCount(0)
  475.         self.DevicesTableWidget.setColumnCount(0)
  476.  
  477.  
  478.     def RefreshButton2_clicked(self):
  479.         self.clearNetworksTable()
  480.         self.networks = {}
  481.         QTimer.singleShot(0, self.showNetworkPage)
  482.  
  483.  
  484.     #
  485.     # CONFIGURE WIFI
  486.     #
  487.  
  488.     def initConfigWifiPage(self):
  489.         self.connect(self.ShowKeyCheckBox, SIGNAL("toggled(bool)"), self.ShowKeyCheckBox_toggled)
  490.  
  491.  
  492.     def showConfigWifiPage(self):
  493.         self.setNextButton(BUTTON_CONNECT)
  494.         self.SSIDLabel.setText(self.network)
  495.         font = QFont()
  496.         font.setPointSize(12)
  497.         self.SSIDLabel.setFont(font)
  498.         self.KeyLineEdit.setText(QString())
  499.         self.ShowKeyCheckBox.setChecked(False)
  500.         self.StrengthIcon.setPixmap(load_pixmap('signal%d' % self.ss, 'other'))
  501.         self.ConfigureIcon.setPixmap(load_pixmap('info', '16x16'))
  502.         self.KeysIcon.setPixmap(load_pixmap('keys', '32x32'))
  503.  
  504.         if 'wpa' in self.security.lower():
  505.             self.WPARadioButton.setChecked(True)
  506.  
  507.         elif 'wep' in self.security.lower():
  508.             self.WEPRadioButton.setChecked(True)
  509.  
  510.         self.KeyLineEdit.setFocus()
  511.         self.KeyLineEdit.setEchoMode(QLineEdit.Password)
  512.         self.displayPage(PAGE_CONFIGURE_WIFI)
  513.  
  514.  
  515.     def ShowKeyCheckBox_toggled(self, b):
  516.         if b:
  517.             self.KeyLineEdit.setEchoMode(QLineEdit.Normal)
  518.         else:
  519.             self.KeyLineEdit.setEchoMode(QLineEdit.Password)
  520.  
  521.  
  522.     #
  523.     # EXIT/CONNECT PAGE
  524.     #
  525.  
  526.     def initExitPage(self):
  527.         self.connect(self.PageSpinBox, SIGNAL("valueChanged(int)"), self.PageSpinBox_valueChanged)
  528.         self.RefreshTimer = QTimer(self)
  529.         self.connect(self.RefreshTimer, SIGNAL('timeout()'), self.RefreshTimer_timeout)
  530.         self.pages = []
  531.         self.page_index = 0
  532.         self.PageSpinBox.setMinimum(1)
  533.  
  534.  
  535.     def showExitPage(self):
  536.         self.setNextButton(BUTTON_FINISH)
  537.         self.NextButton.setEnabled(False)
  538.         self.CancelButton.setEnabled(True)
  539.         self.SSIDLabel_2.setText(QString(self.network))
  540.         self.ip = '0.0.0.0'
  541.         self.hn = ''
  542.         self.success = SUCCESS_NOT_CONNECTED
  543.  
  544.         beginWaitCursor()
  545.         try:
  546.             try:
  547.                 self.ip, _, addressmode, subnetmask, gateway, pridns, sec_dns = wifi.getIPConfiguration(self.dev, self.adaptor_id)
  548.                 vsa_codes = wifi.getVSACodes(self.dev)
  549.                 ss_max, ss_min, ss_val, ss_dbm = wifi.getSignalStrength(self.dev, self.adaptor_id)
  550.                 self.hn = wifi.getHostname(self.dev)
  551.             except Error, e:
  552.                 self.showIOError(e)
  553.                 return
  554.         finally:
  555.             self.dev.close()
  556.             endWaitCursor()
  557.  
  558.         if addressmode.lower() == 'dhcp':
  559.             self.success = SUCCESS_CONNECTED
  560.  
  561.         elif addressmode.lower() == 'autoip':
  562.             self.success = SUCCESS_AUTO_IP
  563.  
  564.         if self.ip == '0.0.0.0':
  565.             self.success = SUCCESS_NOT_CONNECTED
  566.  
  567.         self.pages = []
  568.  
  569.         if self.success == SUCCESS_NOT_CONNECTED:
  570.             self.pages.append((self.__tr("<b>Your printer has not been connected to the wireless network.</b> A valid connection to a wireless network can take up to 2 minutes. This screen will automatically refresh every %1 seconds.<p>If your printer fails to connect within a reasonable time, there may be a problem with your configuration.").arg(REFRESH_INTERVAL), load_pixmap('error', '16x16')))
  571.             self.RefreshTimer.start(REFRESH_INTERVAL * 1000)
  572.  
  573.         elif self.success == SUCCESS_AUTO_IP:
  574.             self.pages.append((self.__tr("Your printer has been connected to the wireless network, but it has been assigned an address which may not be usable."), load_pixmap('warning', '16x16')))
  575.             self.RefreshTimer.start(REFRESH_INTERVAL * 1000)
  576.  
  577.         else: # SUCCESS_CONNECTED
  578.             if self.standalone:
  579.                 self.pages.append((self.__tr("Your printer has been successfully configured on the wireless network. You may now unplug the USB cable. To setup the printer, now run <pre>hp-setup.</pre>"), load_pixmap('info', '16x16')))
  580.             else:
  581.                 self.pages.append((self.__tr("Your printer has been successfully configured on the wireless network. You may now unplug the USB cable."), load_pixmap('info', '16x16')))
  582.             self.CancelButton.setEnabled(False)
  583.             self.BackButton.setEnabled(False)
  584.             self.RefreshTimer.stop()
  585.  
  586.         if addressmode is None:
  587.             self.AddressModeLabel.setText(self.__tr("Unknown"))
  588.         else:
  589.             self.AddressModeLabel.setText(QString(addressmode))
  590.  
  591.         if self.hn is None:
  592.             self.HostnameLabel.setText(self.__tr("Unknown"))
  593.         else:
  594.             self.HostnameLabel.setText(QString(self.hn))
  595.  
  596.         self.IPAddressLabel.setText(QString(self.ip))
  597.         self.GatewayLabel.setText(QString(gateway))
  598.         self.DNSLabel.setText(QString(pridns))
  599.         self.NextButton.setEnabled(True)
  600.  
  601.         self.SignalStrengthLabel.setText(QString("%1/%2 (%3 dBm)").arg(ss_val).arg(ss_max).arg(ss_dbm))
  602.         self.SignalStrengthIcon.setPixmap(load_pixmap('signal%d' % ss_val, 'other'))
  603.  
  604.         for c, s in vsa_codes:
  605.             ss = s.lower()
  606.             if ss.startswith("info"):
  607.                 pixmap = load_pixmap('info', '16x16')
  608.  
  609.             elif ss.startswith("warn"):
  610.                 pixmap = load_pixmap('warning', '16x16')
  611.  
  612.             elif ss.startswith("crit"):
  613.                 pixmap = load_pixmap('error', '16x16')
  614.  
  615.             else:
  616.                 pixmap = load_pixmap('info', '16x16')
  617.  
  618.             self.pages.append((device.queryString("vsa_%s" % str(c).zfill(3)), pixmap))
  619.  
  620.         num_pages = len(self.pages)
  621.         self.PageSpinBox.setMaximum(num_pages)
  622.         self.PageSpinBox.setEnabled(num_pages>1)
  623.         self.PageSpinBox.setValue(1)
  624.         self.PageLabel.setEnabled(num_pages>1)
  625.         self.PageLabel2.setEnabled(num_pages>1)
  626.         self.PageLabel.setText(self.__tr("of %1").arg(num_pages))
  627.         self.page_index = 0
  628.         self.ExitLabel.setText(self.pages[self.page_index][0])
  629.         self.ExitIcon.setPixmap(self.pages[self.page_index][1])
  630.         self.displayPage(PAGE_EXIT)
  631.  
  632.  
  633.     def PageSpinBox_valueChanged(self, i):
  634.         self.page_index = i-1
  635.         self.ExitLabel.setText(self.pages[self.page_index][0])
  636.         self.ExitIcon.setPixmap(self.pages[self.page_index][1])
  637.  
  638.  
  639.     def RefreshTimer_timeout(self):
  640.         self.showExitPage()
  641.  
  642.  
  643.     #
  644.     # ASSOCIATE
  645.     #
  646.  
  647.     def associate(self, key=u''):
  648.         beginWaitCursor()
  649.         try:
  650.             try:
  651.                 alg, mode, secretid = wifi.getCryptoSuite(self.dev)
  652.             except Error, e:
  653.                 self.showIOError(e)
  654.                 return
  655.  
  656.             log.debug("Crypto algorithm: %s" % alg)
  657.             log.debug("Crypto mode: %s" % mode)
  658.         finally:
  659.             endWaitCursor()
  660.  
  661.         beginWaitCursor()
  662.         try:
  663.             try:
  664.                 ret = wifi.associate(self.dev, self.network, self.mode, self.security, key)
  665.             except Error, e:
  666.                 self.showIOError(e)
  667.                 return
  668.         finally:
  669.             endWaitCursor()
  670.  
  671.  
  672.     #
  673.     # Misc
  674.     #
  675.  
  676.     def NextButton_clicked(self):
  677.         p = self.StackedWidget.currentIndex()
  678.         if p == PAGE_INTRO:
  679.             self.showDevicesPage()
  680.  
  681.         elif p == PAGE_DEVICES:
  682.             row = self.DevicesTableWidget.currentRow()
  683.             if row != -1:
  684.                 self.device_uri = self.DevicesTableWidget.item(row, 0).device_uri
  685.                 self.mq = device.queryModelByURI(self.device_uri)
  686.                 back_end, is_hp, bus, model, serial, dev_file, host, zc, port = device.parseDeviceURI(self.device_uri)
  687.                 self.model = models.normalizeModelName(model).lower()
  688.  
  689.             self.showNetworkPage()
  690.  
  691.         elif p == PAGE_NETWORK:
  692.             self.security = 'None'
  693.             self.mode = 'Infrastructure'
  694.             self.ss = 0
  695.             row = self.NetworksTableWidget.currentRow()
  696.             if row != -1:
  697.                 i = self.NetworksTableWidget.item(row, 0)
  698.                 if i is not None:
  699.                     self.network = unicode(i.text())
  700.                     log.debug("Selected network SSID: %s" % self.network)
  701.                     self.n, ok = i.data(Qt.UserRole).toInt()
  702.                     if ok:
  703.                         self.security = self.networks['encryptiontype-%d' % self.n]
  704.                         log.debug("Security: %s" % self.security)
  705.  
  706.                         self.mode = self.networks['communicationmode-%d' % self.n]
  707.                         log.debug("Mode: %s" % self.mode)
  708.  
  709.                         self.ss = self.networks['signalstrength-%d' % self.n]
  710.                         log.debug("Signal strength: %s" % self.ss)
  711.  
  712.             if self.security.lower() != 'none':
  713.                 self.showConfigWifiPage()
  714.             else:
  715.                 # Skip config page if no security to setup
  716.                 self.associate()
  717.                 self.showAssociateProgressDialog()
  718.                 self.showExitPage()
  719.  
  720.         elif p == PAGE_CONFIGURE_WIFI:
  721.             key = unicode(self.KeyLineEdit.text())
  722.             self.associate(key)
  723.             self.showAssociateProgressDialog()
  724.             self.showExitPage()
  725.  
  726.         elif p == PAGE_EXIT:
  727.             if self.dev is not None:
  728.                 self.dev.close()
  729.  
  730.             self.close()
  731.  
  732.         else:
  733.             log.error("Invalid page!") # shouldn't happen!
  734.  
  735.  
  736.     def showAssociateProgressDialog(self):
  737.         AssociateProgressDialog = QProgressDialog(self.__tr("Waiting for printer to connect to the wireless network..."), self.__tr("Cancel"), 0, ASSOCIATE_DELAY, self)
  738.         AssociateProgressDialog.setWindowTitle(self.__tr("HP Device Manager - Please wait..."))
  739.         AssociateProgressDialog.setWindowModality(Qt.WindowModal)
  740.         AssociateProgressDialog.setMinimumDuration(0)
  741.         AssociateProgressDialog.forceShow()
  742.         canceled = False
  743.         for x in range(ASSOCIATE_DELAY):
  744.             AssociateProgressDialog.setValue(x)
  745.             QThread.sleep(1)
  746.             qApp.processEvents()
  747.  
  748.             if AssociateProgressDialog.wasCanceled():
  749.                 canceled = True
  750.                 break
  751.  
  752.         AssociateProgressDialog.setValue(ASSOCIATE_DELAY)
  753.         AssociateProgressDialog.close()
  754.  
  755.         if canceled:
  756.             if self.dev is not None:
  757.                 self.dev.close()
  758.  
  759.             self.close()
  760.  
  761.  
  762.     def BackButton_clicked(self):
  763.         p = self.StackedWidget.currentIndex()
  764.         if p == PAGE_DEVICES:
  765.             self.devices = {}
  766.             self.showIntroPage()
  767.  
  768.         elif p == PAGE_NETWORK:
  769.             self.showDevicesPage()
  770.  
  771.         elif p == PAGE_CONFIGURE_WIFI:
  772.             self.showNetworkPage()
  773.  
  774.         elif p == PAGE_EXIT:
  775.             self.RefreshTimer.stop()
  776.             if self.security.lower() != 'none':
  777.                 self.showConfigWifiPage()
  778.             else:
  779.                 self.showNetworkPage()
  780.  
  781.         else:
  782.             log.error("Invalid page!") # shouldn't happen!
  783.  
  784.  
  785.     def CancelButton_clicked(self):
  786.         if self.dev is not None:
  787.             self.dev.close()
  788.  
  789.         self.close()
  790.  
  791.  
  792.     def displayPage(self, page):
  793.         self.StackedWidget.setCurrentIndex(page)
  794.         self.updateStepText(page)
  795.  
  796.  
  797.     def setNextButton(self, typ=BUTTON_FINISH):
  798.         if typ == BUTTON_NEXT:
  799.             self.NextButton.setText(self.__tr("Next >"))
  800.  
  801.         elif typ == BUTTON_FINISH:
  802.             self.NextButton.setText(self.__tr("Finish"))
  803.  
  804.         elif typ == BUTTON_CONNECT:
  805.             self.NextButton.setText(self.__tr("Connect"))
  806.  
  807.  
  808.     def updateStepText(self, p):
  809.         self.StepText.setText(self.__tr("Step %1 of %2").arg(p+1).arg(self.max_page+1))
  810.  
  811.  
  812.     def showIOError(self, e):
  813.         FailureUI(self, self.__tr("<b>An I/O error occurred.</b><p>Please check the USB connection to your printer and try again.</p>(%1)").arg(QString(e[0])))
  814.  
  815.         if self.dev is not None:
  816.             self.dev.close()
  817.  
  818.         self.close()
  819.  
  820.  
  821.     def __tr(self,s,c = None):
  822.         return qApp.translate("WifiSetupDialog",s,c)
  823.  
  824.  
  825.